#include "../include/header.h"


/**
 * 编写程序A和B。A负责将文件的名字、长度和内容通过管道发送B，
 * B需要新建一个目录，并将该文件存储起来。
 */


bool isEnd(char* str){
	return str[0] == '0' && str[1] == '0' && str[2] == '0';
}
int main(int argc, char* argv[]){
	ARGS_CHECK(argc, 1);
	int fd_pipe  = open("pipe", O_WRONLY);
	ERROR_CHECK(fd_pipe, -1, "open");

	char filename[6] = {0};
	scanf("%s", filename);
	write(fd_pipe, filename, sizeof filename);


	fd_set set;
	FD_ZERO(&set);
	while(1){
		FD_SET(STDIN_FILENO, &set);
		int res_select = select(10, &set, NULL, NULL, NULL);
		ERROR_CHECK(res_select, -1, "select");


		if(FD_ISSET(STDIN_FILENO, &set)){
			char frame[3 + 999] = {0};
			bzero(frame, sizeof frame);

			read(STDIN_FILENO, frame, sizeof frame);
			write(fd_pipe, frame, sizeof frame);
			if(isEnd(frame)){
				printf("写入完毕");
				break;
			}
		}

	}


	return 0;
}
